package com.swing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.config.OrmFile;
import com.entity.OrmFileRes;
import com.entity.OrmJdbcFile;
import com.utils.DruidTool;

public class JdbcUi extends UiJFrame {
	
	DruidTool druidTool = null;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 609156289350641712L;
 
	/*public static void main(String[] args) {
	    SwingUtilities.invokeLater(() -> {
	    	JdbcUi frame = new JdbcUi();
	        frame.setVisible(true);
	    });
	}*/
	public JdbcUi(JFrame pJFrame, DruidTool druidTool) {
		OrmFile.initJdbcConfig();
		setTitle("ORM自动生成_CJL");
		setSize(540, 400);
		//setLocation(400, 400);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null); // 居中显示
        //setAlwaysOnTop(true);//将弹窗放在最前面
		setLayout(new GridLayout(10, 2));

		JPanel titleJPanel = new JPanel();
		
		JPanel driverJPanel = new JPanel();
		JPanel ipJPanel = new JPanel();
		JPanel dbJPanel = new JPanel();
		JPanel nameJPanel = new JPanel();
		JPanel pswJPanel = new JPanel();
		JPanel btnJPanel = new JPanel();
        
		JLabel titleLabel = new JLabel("数据库配置", JLabel.CENTER);
		
		JLabel driverLabel = new JLabel("驱动", JLabel.LEFT);
		driverLabel.setPreferredSize(new Dimension(100,30));
		JLabel ipLabel = new JLabel("地址", JLabel.LEFT);
		ipLabel.setPreferredSize(new Dimension(100,30));
		JLabel dbLabel = new JLabel("数据库", JLabel.LEFT);
		dbLabel.setPreferredSize(new Dimension(100,30));
		JLabel nameLabel = new JLabel("用户名", JLabel.LEFT);
		nameLabel.setPreferredSize(new Dimension(100,30));
		JLabel pswLabel = new JLabel("密码", JLabel.LEFT);
		pswLabel.setPreferredSize(new Dimension(100,30));
		
		setVisible(true);
		pJFrame.setEnabled(false);
    	OrmFileRes<OrmJdbcFile> res = OrmFile.readJdbc();
    	if (res.getStatus() == 0) {
    		error("数据库配置读取失败");
    		setVisible(false);
    		return;
		}

		JTextField driverField = new JTextField(res.getData().getDriver(), 30);
		driverField.setPreferredSize(new Dimension(300,30));
        JTextField ipField = new JTextField(res.getData().getUrl(), 30);
        ipField.setPreferredSize(new Dimension(300,30));
        JTextField dbField = new JTextField(res.getData().getDatabase(), 30);
        dbField.setPreferredSize(new Dimension(300,30));
        JTextField nameField = new JTextField(res.getData().getUser(), 30);
        nameField.setPreferredSize(new Dimension(300,30));
        JTextField pswField = new JTextField(res.getData().getPassword(), 30);
        pswField.setPreferredSize(new Dimension(300,30));

		JButton saveBtn = new JButton("保存");
		saveBtn.setBackground(new Color(220, 53, 69));
		saveBtn.setForeground(Color.WHITE);
		
		JButton closeBtn = new JButton("返回");
		closeBtn.setBackground(Color.WHITE);

		titleJPanel.add(titleLabel);

		driverJPanel.add(driverLabel);
		driverJPanel.add(driverField);

		ipJPanel.add(ipLabel);
		ipJPanel.add(ipField);

		dbJPanel.add(dbLabel);
		dbJPanel.add(dbField);

		nameJPanel.add(nameLabel);
		nameJPanel.add(nameField);

		pswJPanel.add(pswLabel);
		pswJPanel.add(pswField);

		
		btnJPanel.add(saveBtn);
		btnJPanel.add(closeBtn);

		add(titleJPanel);
		add(driverJPanel);
		add(ipJPanel);
		add(dbJPanel);
		add(nameJPanel);
		add(pswJPanel);
		
		add(btnJPanel);
		
		setVisible(true);
		saveBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String driverVal =  driverField.getText();
				String ipVal     =  ipField.getText();
				String dbVal     =  dbField.getText();
				String nameVal   =  nameField.getText();
				String pswVal    =  pswField.getText();
				
				OrmFile.saveJdbc(driverVal,ipVal,dbVal,nameVal,pswVal);
				
				ok("保存成功");
				setVisible(false);
				pJFrame.setEnabled(true);
				if (null != druidTool) {
					druidTool.close();
				}
			}
		});
		closeBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				pJFrame.setEnabled(true);
			}
		});

	}
}
